using System;
using System.Text;
using InfoJetSoft.Service.XPath.Tokens;

//CID 0164 //2008.12.13 //Begin
namespace InfoJetSoft.Service.XPath.Exprs
{
	/// <summary>
	/// Summary description for VariableReference.
	/// </summary>
    public class VariableReference : IExpr
	{
		private string _name = null;

		public VariableReference()
		{
			//
			// TODO: Add constructor logic here
			//
        }

        public object Evaluate(ExprContext context)
        {
            //CID 0824 //2011.03.03 //Begin
            return context.Calculator.ResolveVariable(this._name);
            //CID 0824 //2011.03.03 //End
        }

        //CID 0866 //2011.04.01 //Begin
        public void Visit(IExprVisitor visitor)
        {
        }
        //CID 0866 //2011.04.01 //End

        public void Print(StringBuilder buffer)
        {
            buffer.Append("$");
            buffer.Append(this._name);
        }

		public static VariableReference Parse( ExprParser parser )
		{
			Token nextToken = parser.LookupNextToken(1);
			if( nextToken != null && nextToken.Type.Equals( TokenType.Symbol ) && nextToken.Value.Equals( "$" ) )
			{
				Token nextToken2 = parser.LookupNextToken(2);
				if( nextToken2 != null && (nextToken2.Type.Equals( TokenType.NCName ) || nextToken2.Type.Equals( TokenType.QName ) ) )
				{
					parser.MoveNextIndex(2);
					
					VariableReference variableReference = new VariableReference();
					variableReference._name = nextToken2.Value;
					return variableReference;
				}
			}
			return null;
		}
	}
}
//CID 0164 //2008.12.13 //End
